package com.liang.ui;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.liang.utils.TimeUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * @ClassName mailBoxFrame
 * @Description TODO 显示邮件内容
 * @Author liang
 * @Date 2019/4/10 21:23
 * @Version 1.0
 **/
public class MailBoxFrame extends JFrame {
    private static JFrame jf = new JFrame("邮件内容");
    private static JTable table = new JTable();
    private JButton exp = new JButton("导出当日邮件");
    private static   String[] columnNames = {"日期" , "姓名" , "序号" , "发件人" , "邮件主题" , "处理状态" , "备注"  };//列


    public MailBoxFrame(String[][] rowData){
        //设置窗体的位置及大小
        jf.setBounds(300, 200, 3000, 500);
        //设置关闭
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        table = new JTable(rowData , columnNames);
        //设置列宽度
        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.getColumnModel().getColumn(1).setPreferredWidth(60);
        table.getColumnModel().getColumn(2).setPreferredWidth(30);
        table.getColumnModel().getColumn(3).setPreferredWidth(60);
        table.getColumnModel().getColumn(4).setPreferredWidth(130);
        table.getColumnModel().getColumn(5).setPreferredWidth(90);
        table.getColumnModel().getColumn(6).setPreferredWidth(60);


        /*按钮部分--South*/
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(exp);
        jf.add(buttonPanel, "South");

        //将JTable对象放在滚动面板中，并将该JScrollPane放在窗口中显示出来
        jf.add(new JScrollPane(table));
        //调整窗口大小
        jf.pack();

        //监听按钮
        action(rowData);
        //设置窗体可见
        jf.setVisible(true);

    }



    /**
     * 导出到excel
     * 生成新的excel文件
     * 直接在当前目录下生成
     * @param rowData
     */
    private void action(final String[][] rowData) {
        exp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //数据内容转成Map格式,导出到excel中
                    String filePath = tabToExcel(rowData);
                    JOptionPane.showMessageDialog(null, "成功:" + filePath);

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "导出失败:"+ex.getMessage());
                    System.exit(0);
                }

            }

            /**
             * 将页面的内容复制到excel中
             *
             * @param rowData
             */
            private String tabToExcel(String[][] rowData) {
                String today = TimeUtils.getToday();
                String name = rowData[rowData.length-1][1];
                String filePath = "d:/邮件采集/邮件采集_"+name+"_"+today+".xlsx";
                //整个excel内容
                ArrayList<Object> arrayList = CollUtil.newArrayList();
                ArrayList<String>  titleList= CollUtil.newArrayList("日期", "姓名", "序号", "发件人", "邮件主题", "处理状态", "备注");
                arrayList.add(titleList);

                for(int i = 0; i < rowData.length ; i ++){
                    //行内容 , 一个List代表一行数据
                    ArrayList<Object> list = CollUtil.newArrayList();
                    for(int j = 0 ; j < rowData[i].length ; j ++){
                        //行+列  单元格内容
                        list.add(rowData[i][j]);
                    }
                    arrayList.add(list);
                }
                ExcelWriter writer = ExcelUtil.getWriter(filePath);
                writer.write(arrayList);
                //设置列宽
                writer.setColumnWidth(0,12);
                writer.setColumnWidth(1,8);
                writer.setColumnWidth(2,4);
                writer.setColumnWidth(3,8);
                writer.setColumnWidth(4,55);
                writer.setColumnWidth(5,10);
                writer.setColumnWidth(6,8);
                CellStyle cellStyle = writer.getCellStyle();
                //设置无边框
                cellStyle.setBorderBottom(BorderStyle.NONE);
                cellStyle.setBorderLeft(BorderStyle.NONE);
                cellStyle.setBorderRight(BorderStyle.NONE);
                cellStyle.setBorderTop(BorderStyle.NONE);


                writer.close();
                return filePath;
            }
        });
    }




}
